﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：AjaxResponseOfTResult.cs
// 功能描述：TongTongMall.WebApi
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TongTongMall.WebApi.Models
{
    /// <summary>
    /// ajax的请求定义标准的返回格式
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    public class AjaxResponse<TResult>
    {
        /// <summary>
        /// 成功的状态
        /// </summary>
        [JsonIgnore]
        public bool Success { get; set; }

        /// <summary>
        /// 实际结果ajax请求的对象
        /// </summary>
        [JsonIgnore]
        public TResult Result { get; set; }

        /// <summary>
        /// 错误的内容
        /// </summary>
        [JsonIgnore]
        public ErrorInfo Error { get; set; }

        public int Code { get; private set; }

        public string Msg { get; private set; } = "成功";

        public object Data { get; private set; } = new object();


        public AjaxResponse(TResult result)
        {
            Result = result;
            Success = true;

            Code = 1100;
            Data = result == null ? new object() : result;
        }

        public AjaxResponse(TResult result, string message)
        {
            Result = result;
            Success = true;

            Code = 1100;
            Data = result;
            Msg = message;
        }

        public AjaxResponse()
        {
            Success = true;
            Code = 1100;
        }


        public AjaxResponse(ErrorInfo error, bool unAuthorizedRequest = false)
        {
            Error = error;
            Success = false;

            Code = error.Code == 0 ? 1103 : error.Code;
            Msg = error.Message;
            Data = error.Data ?? new object();
        }
    }
}
